package com.boot.utils;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/15.
 */
@Slf4j
public class HttpUtil {


	public static String reqGet(String url){
		HttpClient client=new DefaultHttpClient();
		client.getParams().setIntParameter("http.socket.timeout", 1000 * 60);
		String respBody="";
		try {
			HttpGet get =new HttpGet(url);
			HttpClientParams.setCookiePolicy(client.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);
			HttpResponse resp = client.execute(get);
			if(resp.getStatusLine().getStatusCode() == 200){
				HttpEntity entity =resp.getEntity();
				respBody = EntityUtils.toString(entity).toString();
			}else{
				respBody="";
			}
		}catch (ClientProtocolException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			client.getConnectionManager().shutdown();
		}
		return respBody;
	}



	public static boolean reqGetRange(String url){
		boolean acceptRanges=false;
		Long s = System.currentTimeMillis();
		HttpClient client=new DefaultHttpClient();
		HttpHead httpHead = new HttpHead(url);
		HttpResponse resp;
		try {
			resp = client.execute(httpHead);
			if(resp.getStatusLine().getStatusCode() == 200){
				Header[] headers=resp.getHeaders("Content-Length");
				if(headers.length >0){
					Long contentLength = Long.valueOf(headers[0].getValue());
					httpHead.abort();
					httpHead = new HttpHead(url);
					httpHead.addHeader("Range", "bytes=0-"+(contentLength-1));
					resp = client.execute(httpHead);
					if(resp.getStatusLine().getStatusCode() == 206){
						acceptRanges = true;
					}
					httpHead.abort();
				}
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			client.getConnectionManager().shutdown();
		}
		Long e = System.currentTimeMillis();
		log.info(MessageFormat.format("请求经过:{0}s", e-s));
		return acceptRanges;
	}

	public static void main(String[] args) {

		String content = reqGet("http://user.api.autohome.com.cn/api/UserInfo/GetUserInfo?_appid=app&userid=73649548");
		JSONObject json= JSON.parseObject(content);
		int returncode=(int)json.get("returncode");
		Map<String,Object> result= (Map) json.get("result");
		System.out.println(result.get("HeadImage"));
		System.out.println(result.get("RealName"));
		System.out.println(result.get("NickName"));

		System.out.println(content);
	}
}
